﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Boxie
{
    class BoxieHashLib
    {
        public static void DumpMD5Hashes()
        {
            int c = Boxie.frmBoxie.instance.appargs.Length;
            string[] a = Boxie.frmBoxie.instance.appargs;
            string output = "Boxie MD5 Dump:\n---------------\n\n";
            byte[] oHashValue;
            MD5CryptoServiceProvider oMD5Hasher = new MD5CryptoServiceProvider();
            FileStream oFileStream = null;
            for (int i = 0; i < c; i++)
            {
                try
                {
                    oFileStream = new FileStream(a[i], FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    oHashValue = oMD5Hasher.ComputeHash(oFileStream);
                    oFileStream.Close();
                    string md5 = System.BitConverter.ToString(oHashValue);
                    md5 = md5.Replace("-", "");
                    output += a[i] + " : " + md5 + "\n";
                }
                catch (Exception e)
                {
                    output += a[i] + " : Error\n";
                }
            }
            // output file
            output += "\n---------------";
            output = output.Replace("\n", System.Environment.NewLine);
            string fn = System.Windows.Forms.Application.StartupPath + "\\md5dump_" + DateTime.Now.ToFileTimeUtc().ToString() + ".txt";
            TextWriter tw = new StreamWriter(fn);
            tw.Write(output);
            tw.Close();
            // clear queue
            Boxie.frmBoxie.instance.ClearEntireQueue();
            // add this to queue
            frmBoxie.instance.appargs = new string[1] { fn };
            if (frmBoxie.instance.fQ != null)
                if (frmBoxie.instance.fQ.Visible)
                {
                    frmBoxie.instance.fQ.lst.Items.Clear();
                    frmBoxie.instance.fQ.lst.Items.Add(fn);
                }
            // update Boxie title
            frmBoxie.instance.Text = frmBoxie.instance.appnamebuild + "1 Files Loaded";
        }
    }
}
